#include <iostream>
#include <vector>
#include <string>
#include <boost/filesystem.hpp>
#include "util.hpp"
const std::string src_path = "data/input";
const std::string output = "data/raw_html/raw.txt";

enum ErrorInfo
{
    ENUMERROR = 1,
    PARSEERROR,
    SAVEERROR,
	PATHERROR
};

typedef struct DocInfo
{
    std::string title;
    std::string content;
    std::string url;
}DocInfo_t;

bool EnumFile(const std::string &src_path, std::vector<std::string> *files_list)//将src_path目录下文件添加到files_list中
{
	namespace fs = boost::filesystem;
	fs::path root_path(src_path);
	if (!fs::exists(root_path))
	{
		std::cerr << "File path:" << src_path << " does not exist" << std::endl;
		return false;
	}
	fs::recursive_directory_iterator end;
	for (fs::recursive_directory_iterator iter(root_path); iter != end; iter++)
	{
		if (!fs::is_regular_file(*iter)) continue;
		if (iter->path().extension() != ".html") continue;
		//std::cout << iter->path().string() << std::endl;
		files_list->push_back(iter->path().string());
	}
	return true;
}

static bool ParseTitle(const std::string &str, std::string *title)
{
	int begin = str.find("<title>");
	if (begin == std::string::npos) return false;
	
	int end = str.find("</title>");
	if (end == std::string::npos) return false;
	
	begin += std::string("<title>").size();
	if (begin > end)
	{
		return false;
	}
	*title = str.substr(begin, end - begin);
	return true;
}
static bool ParseContent(const std::string &str, std::string *content)
{
	enum status
	{
		LABLE,
		CONTENT
	};
	enum status s = LABLE;
	for (auto ch : str)
	{
		switch(s)
		{
		case LABLE:
			if (ch == '>') s = CONTENT; 
			break;
		case CONTENT:
			if (ch == '<') s = LABLE;
			else
			{
				if (ch == '\n') ch = ' ';
				content->push_back(ch);
			}
			break;
		default:
			break;
		}
	}
	return true;
}
bool ParseUrl(const std::string &path, std::string *url)
{
	std::string url_head = "https://www.boost.org/doc/libs/1_85_0/doc/html";
	std::string url_tail = path.substr(src_path.size());
	*url = url_head + url_tail;
    return true;
}

void ShowDoc(const DocInfo_t &doc)
{
	std::cout << "title:\t " << doc.title << std::endl;
	std::cout << "content:\t " << doc.content << std::endl;
	std::cout << "url:\t " << doc.url << std::endl;
}

bool ParseHtml(std::vector<std::string> &files_list, std::vector<DocInfo_t>* results)//将files_list中各文档元素的解析结果添加到results中
{
	for (const std::string &str : files_list)
	{
		std::string result;//将文件中的信息全部读到result字符串中，再对result字符串进行分割
		if (!ns_util::FileUtil::ReadFile(str, &result)) continue;
		DocInfo_t doc;
		if (!ParseTitle(result, &doc.title)) continue;
		if (!ParseContent(result, &doc.content)) continue;
		if (!ParseUrl(str, &doc.url)) continue;
		
		results->push_back(std::move(doc));
		//ShowDoc(doc);
	}
	return true;
}

bool SaveHtml(std::vector<DocInfo_t> &results, const std::string &output)//将results中的解析结果保存到output中
{
#define SEP '\3'
	std::ofstream out(output, std::ios::out | std::ios::binary);
	if (!out.is_open())
	{
		std::cerr << "SavaHtml: open " << output  << "failed" << std::endl;
		return false;
	}
	for (auto &iter : results)
	{
		std::string out_string;
		out_string += iter.title;
		out_string += SEP;
		out_string += iter.content;
		out_string +=SEP;
		out_string += iter.url;
		out_string += '\n';

		out.write(out_string.c_str(), out_string.size());
	}
	out.close();
	return true;
}

//const &: 输入
//*: 输出
//&：输入输出

int main()
{
    std::vector<std::string> files_list;
    if (!EnumFile(src_path, &files_list))
    {
        std::cerr << "Enum File error" << std::endl;
        return ENUMERROR;
    }
   std::vector<DocInfo_t> results;
    if (!ParseHtml(files_list, &results))
    {
        std::cerr << "Parse Html eorror" << std::endl;
        return PARSEERROR;
    }
    if (!SaveHtml(results, output))
    {
        std::cerr << "Save Html error" << std::endl;
        return SAVEERROR;
    }
    return 0;
}

